// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="SPTPageObjectBase.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.ShareTest.SharePoint2010.PageObjects
{
    #region

    using System;
    using System.Diagnostics;
    using System.IO;

    using Collaboris.ShareTest.Core;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Remote;
    using OpenQA.Selenium.Support.PageObjects;

    #endregion

    /// <summary>
    /// 	The spt page object base.
    /// </summary>
    public class SPTPageObjectBase
    {
        #region Constants and Fields

        /// <summary>
        /// 	The full page url.
        /// </summary>
        protected string fullPageUrl;

        /// <summary>
        /// 	The web driver.
        /// </summary>
        protected RemoteWebDriver webDriver;

        private string xPathToWaitOnLoad;

        #endregion

        #region Constructors and Destructors

        public SPTPageObjectBase(RemoteWebDriver driver, string fullUrl)
            : this(driver, fullUrl, String.Empty)
        {
        }

        public SPTPageObjectBase(RemoteWebDriver driver, string fullUrl, string loginFileName)
            : this(driver, fullUrl, String.Empty, loginFileName)
        {
        }

        /// <summary>
        /// 	Initializes a new instance of the <see cref = "SPTPageObjectBase" /> class.
        /// </summary>
        /// <param name = "driver">
        /// 	The Driver.
        /// </param>
        /// <param name = "settings"></param>
        /// <param name = "fullUrl">
        /// 	The fullUrl.
        /// </param>
        /// <param name = "verifyElementOnLoad">
        /// 	The verify Element On Load.
        /// </param>
        /// <param name = "loginFileName"></param>
        public SPTPageObjectBase(
            RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad, string loginFileName)
        {
            Logger.LogMessage("=================================================================");
            Logger.LogMessage("Creating an instance of " + this.GetType().FullName);

            this.webDriver = driver;
            this.XPathToWaitOnLoad = verifyElementOnLoad;

            this.fullPageUrl = fullUrl;

            // logon with user (using AutoIt)
            string autoItLogonExe = loginFileName;
            if (!string.IsNullOrEmpty(autoItLogonExe))
            {
                this.LogonAsUser(driver, autoItLogonExe);
            }

            this.RefreshPage();

            if (RunSettings.SiteInCloudShare && ! RunSettings.AuthenticatedToCloudShare)
            {
                driver.WaitForElementNotPresent(By.LinkText("Proceed to your environment"));
                driver.FindElement(By.LinkText("Proceed to your environment")).Click();

                RunSettings.AuthenticatedToCloudShare = true;
            }

            if (! String.IsNullOrEmpty(this.XPathToWaitOnLoad))
            {
                this.webDriver.WaitForElementNotPresent(By.XPath(this.XPathToWaitOnLoad));
            }

            PageFactory.InitElements(this.webDriver, this);
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// 	Gets or sets Title.
        /// </summary>
        [FindsBy(How = How.Id, Using = "Title")]
        public IWebElement Title { get; set; }

        #endregion

        #region Properties

        protected string XPathToWaitOnLoad
        {
            get
            {
                if (String.IsNullOrEmpty(this.xPathToWaitOnLoad))
                {
                    this.xPathToWaitOnLoad = Common.Map.Global.XPathDefaultBodyClass;
                }
                return this.xPathToWaitOnLoad;
            }
            set
            {
                this.xPathToWaitOnLoad = value;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 	The logon as user.
        /// </summary>
        /// <param name = "driver">
        /// 	The driver.
        /// </param>
        /// <param name = "autoItFileName"></param>
        public void LogonAsUser(IWebDriver driver, string autoItFileName)
        {
            string fullPath = null;

            if (driver is FirefoxDriver)
            {
                fullPath = Path.Combine(RunSettings.AutoItDirectory + @"\Firefox", autoItFileName);
            }

            if (driver is InternetExplorerDriver)
            {
                fullPath = Path.Combine(RunSettings.AutoItDirectory + @"\InternetExplorer", autoItFileName);
            }

            if (!String.IsNullOrEmpty(fullPath))
            {
                Process.Start(fullPath);
            }
        }

        public virtual void RefreshPage()
        {
            if (this.webDriver == null)
            {
                throw new NullReferenceException("issues");
            }
            this.webDriver.Navigate().GoToUrl(this.fullPageUrl);
            
        }

        #endregion
    }
}